import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:smart_devices/model/inventory.dart';
import 'package:smart_devices/routes/index.dart';
import 'package:smart_devices/ui/scroll_refresh_config.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';

class InventoryController extends GetxController {
  RefreshController refreshController = RefreshController();

  RxList<InventoryModel> pageList = RxList([]);

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    pageList.add(InventoryModel(
        title: '电梯设备盘点任务202',
        deptName: '生产部',
        charge: '赵子龙',
        startPlan: '2022-07-18 10:30',
        endPlan: '2022-07-18 12:00',
        schedule: Random.secure().nextInt(10)));

    /// 添加
    refreshController.loadComplete();
  }

  @override
  void onInit() {
    super.onInit();
    for (var i = 0; i < 10; i++) {
      pageList.add(InventoryModel(
          title: '电梯设备盘点任务202',
          deptName: '生产部',
          charge: '赵子龙',
          startPlan: '2022-07-18 10:30',
          endPlan: '2022-07-18 12:00',
          schedule: Random.secure().nextInt(10)));
    }
  }

  @override
  void onClose() {
    super.onClose();
    refreshController.dispose();
    Get.delete();
  }
}

class InventoryComponent extends GetView<InventoryController> {
  const InventoryComponent({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        builder: (_) {
          return Obx(() => scrollRefreshConfig(context));
        },
        init: Get.put(InventoryController()));
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  Widget listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext c, int index) {
        return Container(
          margin: const EdgeInsets.only(top: 10),
          color: Colors.white,
          padding: const EdgeInsets.only(left: 20),
          child: Column(
            children: [
              Container(
                height: 40,
                alignment: Alignment.center,
                child: Text(
                  controller.pageList[index].title,
                  style: StylesUtils.customTextStyle(
                      fontWeight: FontWeight.w700, color: ColorsUtils.color0052, fontSize: 16),
                ),
              ),
              Row(
                children: [
                  Container(
                    width: 60,
                    height: 30,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '部门',
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      controller.pageList[index].deptName,
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                    ),
                  ))
                ],
              ),
              Row(
                children: [
                  Container(
                    width: 60,
                    height: 30,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '负责人',
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      controller.pageList[index].charge,
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                    ),
                  ))
                ],
              ),
              Row(
                children: [
                  Container(
                    width: 60,
                    height: 30,
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '计划时间',
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      '${controller.pageList[index].startPlan}至${controller.pageList[index].endPlan}',
                      style:
                          StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                    ),
                  ))
                ],
              ),
              Container(
                height: 30,
                padding: const EdgeInsets.only(right: 20),
                alignment: Alignment.centerLeft,
                child: Row(
                  children: [
                    Expanded(
                        child: Container(
                      alignment: Alignment.centerLeft,
                      child: LinearProgressIndicator(
                        minHeight: 10,
                        borderRadius: BorderRadius.circular(5),
                        backgroundColor: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                        value: controller.pageList[index].schedule / 10.0,
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
                      ),
                    )),
                    Container(
                      alignment: Alignment.centerLeft,
                      padding: const EdgeInsets.only(left: 10),
                      child: Text(
                        '${controller.pageList[index].schedule}/10',
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.color9999, fontSize: 13),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        );
      },
      itemCount: controller.pageList.length,
      itemExtent: 170,
    );
  }
}

class InventoryState extends StatefulWidget {
  const InventoryState({super.key});

  @override
  State<InventoryState> createState() => _InventoryState();
}

class _InventoryState extends State<InventoryState> {
  RefreshController refreshController = RefreshController();

  List<InventoryModel> pageList = [];

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 15) {
      return refreshController.loadNoData();
    }

    pageList.add(InventoryModel(
        title: '电梯设备盘点任务202',
        deptName: '生产部',
        charge: '赵子龙',
        startPlan: '2022-07-18 10:30',
        endPlan: '2022-07-18 12:00',
        schedule: Random.secure().nextInt(10)));

    /// 添加
    refreshController.loadComplete();
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 10; i++) {
      pageList.add(InventoryModel(
          title: '电梯设备盘点任务202',
          deptName: '生产部',
          charge: '赵子龙',
          startPlan: '2022-07-18 10:30',
          endPlan: '2022-07-18 12:00',
          schedule: Random.secure().nextInt(10)));
    }
  }

  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return scrollRefreshConfig(context);
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: refreshController,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: listViewBuilder(context),
    );
  }

  Widget listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext c, int index) {
        return InkWell(
          onTap: () {
            Get.toNamed(Routes.deviceInventoryTask);
          },
          child: Container(
            margin: const EdgeInsets.only(top: 10),
            color: Colors.white,
            padding: const EdgeInsets.only(left: 20),
            child: Column(
              children: [
                Container(
                  height: 40,
                  alignment: Alignment.center,
                  child: Text(
                    pageList[index].title,
                    style: StylesUtils.customTextStyle(
                        fontWeight: FontWeight.w700, color: ColorsUtils.color0052, fontSize: 16),
                  ),
                ),
                Row(
                  children: [
                    Container(
                      width: 60,
                      height: 30,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '部门',
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                      ),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        pageList[index].deptName,
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                      ),
                    ))
                  ],
                ),
                Row(
                  children: [
                    Container(
                      width: 60,
                      height: 30,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '负责人',
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                      ),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        pageList[index].charge,
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                      ),
                    ))
                  ],
                ),
                Row(
                  children: [
                    Container(
                      width: 60,
                      height: 30,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '计划时间',
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.color3333, fontSize: 13),
                      ),
                    ),
                    Expanded(
                        child: Container(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '${pageList[index].startPlan}至${pageList[index].endPlan}',
                        style:
                            StylesUtils.customTextStyle(color: ColorsUtils.colorAaaa, fontSize: 13),
                      ),
                    ))
                  ],
                ),
                Container(
                  height: 30,
                  padding: const EdgeInsets.only(right: 20),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: [
                      Expanded(
                          child: Container(
                        alignment: Alignment.centerLeft,
                        child: LinearProgressIndicator(
                          minHeight: 10,
                          borderRadius: BorderRadius.circular(5),
                          backgroundColor:
                              ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                          value: pageList[index].schedule / 10.0,
                          color: ColorsUtils.parseColorFromHexString(ColorsUtils.color0052),
                        ),
                      )),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: const EdgeInsets.only(left: 10),
                        child: Text(
                          '${pageList[index].schedule}/10',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color9999, fontSize: 13),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        );
      },
      itemCount: pageList.length,
      itemExtent: 170,
    );
  }
}
